<?php 

/**
 * 个人中心控制器
 * @author 阿柱 <422852954@qq.com>
 */
class MyselfController extends CommonController{
	private $uid=0;
	public function __init(){
		if(!session('uid') || !session('uname')){
			go(U('index/index/index'));
		}
		$this->uid=session('uid');
	}
    /**
     *   **列表展示方法
     */
    public function index(){
    	$this->memberTop();
		
        $this->display(); 
    }

	
	/**
	 * 个人中心
	 */
	public function myself(){
		if(IS_POST){
			$model=K('User');
			if(!$model->editMyself())   $this->error($model->error);
			$this->success('编辑成功','',1);
		}
		$this->memberTop();
		$this->display();
		
	}
	
	/**
	 * 我的订单
	 */
	public function myOrder(){
		$this->memberTop();
		$unpaid=K('Order')->where("status='未付款' AND uid={$this->uid}")->getField('oid',true);
		foreach ($unpaid as $k => $v) {
			$unpaid[$k]=M()->join('__order_list__ ol JOIN __goods__ g ON ol.gid=g.gid ')->where("ol.oid={$v}")->all();
			foreach ($unpaid[$k] as $key=>$value) {
				$unpaid[$k][$key]['unitPrice']=number_format($value['subtotal']/$value['quantity'],2);
				$unpaid[$k][$key]['combine']=K('GoodsList')->where("number='{$value['number']}'")->getField('combine');
	//			颜色和尺码
				$arr=explode(',', $unpaid[$k][$key]['combine']);
				foreach ($arr as $gaid) {
					$spec=M()->join('__goods_attr__ ga JOIN __type_attr__ ta ON ga.taid=ta.taid')->where("gaid={$gaid}")->getField('aname');
					$unpaid[$k][$key][String::pinyin($spec)]=K('GoodsAttr')->where("gaid={$gaid}")->getField('gvalue');
				}
			}
		}
		$this->assign('unpaid',$unpaid);
		
		$this->display();
	}
	
	/**
	 * 取消订单
	 */
	public function delOrder(){
		$oid=Q('get.oid',0,'intval');
		K('Order')->where("oid={$oid}")->del();
		K('OrderList')->where("oid={$oid}")->del();
		go(U('index/Myself/myOrder/#userInfoNav'));
	}
	/**
	 * 待发货
	 */
	public function noSend(){
		$this->memberTop();
		
		
		
		$this->display();
	}
	
	/**
	 * 收货地址
	 */
	public function myAddress(){
		if(IS_POST){
			$model=K('Address');
			if(!$model->addAddress())  $this->error($model->error);
			$this->success('编辑成功');
			go(U('myAddress'));
		}
		$this->memberTop();
//		$addressData=K('Address')->all();
//		p($addressData);
		$this->assign('addressData',K('Address')->where("uid={$this->uid}")->all());
		$this->display();
	}
	/**
	 * 删除收货地址
	 */
	public function delAddress(){
		$aid=Q('get.aid',0,'intval');
		K('Address')->where("aid={$aid}")->del();
		go(U('myAddress'));
	}
	/**
	 * 异步编辑收货地址
	 */
	public function ajaxEditAddress(){
		$aid=Q('post.aid',0,'intval');
		$data=K('Address')->where("aid={$aid}")->find();
		$arr=explode(',', $data['area']);
		$data['s1']=$arr[0];
		$data['s2']=$arr[1];
		$data['s3']=$arr[2];
		$this->ajax($data);
	}
	/**
	 * 编辑收货地址
	 */
	public function editAddress(){
		if(IS_POST){
			$model=K('Address');
			if(!$model->editAddress())   $this->error($model->error);
			$this->success('编辑成功',U("myAddress"));
		}
	}
	/**
	 * 我的最爱
	 */
	public function myLove(){
		$this->memberTop();
		$uid=session('uid');
		$data=K('Love')->where("uid={$uid}")->all();
		$loveData=array();
		foreach ($data as $k => $v) {
			$temp=K('Goods')->where("gid={$v['gid']}")->find();
			$temp['lid']=$v['lid'];
			$loveData[]=$temp;			
		}
		$this->assign('loveData',$loveData);
		$this->display();
	}
	
	private function memberTop(){
		$uid=session('uid');
		$userData=K('User')->where("uid={$uid}")->find();
		$this->assign('userData',$userData);
		$this->assign('loveNum',K('Love')->where("uid={$uid}")->count());
		$this->topCate();
	}
	/**
	 * 修改密码
	 */
	public function editPassword(){
		$pwd=K('User')->where("uid={$this->uid}")->getField('password');
//		密码不正确
		if(md5(Q('post.current'))!=$pwd)   $this->error('旧密码不正确');
		if(Q('post.password1')!=Q('post.password2'))  $this->error('两次密码不一致');
//		修改密码
		K('User')->where("uid={$this->uid}")->update(array('password'=>md5(Q('post.password1'))));
		$this->success("密码修改成功,请牢记密码{$_POST['password1']}");
	}
}
 

 
 
 
 
 
 







 ?>